package com.kulian.Signalling.system.service.impl.socket;

import com.kulian.Signalling.system.mode.socket.SignalingMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 房间管理服务
 */
@Slf4j
@Component
public class RoomManager {
    // 房间映射表 key: roomId, value: 房间信息
    private final Map<String, Room> rooms = new ConcurrentHashMap<>();
    
    // 用户到房间的映射 key: username, value: roomId
    private final Map<String, String> userToRoomMap = new ConcurrentHashMap<>();

    /**
     * 创建房间
     * @param creator 创建者用户名
     * @return 房间ID
     */
    public String createRoom(String creator) {
        String roomId = UUID.randomUUID().toString();
        Room room = new Room(roomId, creator);
        rooms.put(roomId, room);
        userToRoomMap.put(creator, roomId);
        log.info("用户[{}]创建了房间[{}]", creator, roomId);
        return roomId;
    }

    /**
     * 加入房间
     * @param roomId 房间ID
     * @param username 用户名
     * @return 是否成功加入
     */
    public boolean joinRoom(String roomId, String username) {
        Room room = rooms.get(roomId);
        if (room == null) {
            return false;
        }
        room.addParticipant(username);
        userToRoomMap.put(username, roomId);
        log.info("用户[{}]加入了房间[{}]", username, roomId);
        return true;
    }

    /**
     * 离开房间
     * @param username 用户名
     */
    public void leaveRoom(String username) {
        String roomId = userToRoomMap.get(username);
        if (roomId != null) {
            Room room = rooms.get(roomId);
            if (room != null) {
                room.removeParticipant(username);
                if (room.isEmpty()) {
                    rooms.remove(roomId);
                    log.info("房间[{}]因无人被销毁", roomId);
                }
            }
            userToRoomMap.remove(username);
            log.info("用户[{}]离开了房间[{}]", username, roomId);
        }
    }

    /**
     * 获取房间信息
     * @param roomId 房间ID
     * @return 房间信息
     */
    public Room getRoom(String roomId) {
        return rooms.get(roomId);
    }

    /**
     * 获取用户所在房间
     * @param username 用户名
     * @return 房间ID
     */
    public String getUserRoom(String username) {
        return userToRoomMap.get(username);
    }

    public boolean isUserOnline(String username) {
        return userToRoomMap.containsKey(username);
    }

    /**
     * 房间信息类
     */
    public static class Room {
        private final String roomId;
        private final String creator;
        private final Set<String> participants = ConcurrentHashMap.newKeySet();
        private final Date createdAt = new Date();

        public Room(String roomId, String creator) {
            this.roomId = roomId;
            this.creator = creator;
            this.participants.add(creator);
        }

        public void addParticipant(String username) {
            participants.add(username);
        }

        public void removeParticipant(String username) {
            participants.remove(username);
        }

        public boolean isEmpty() {
            return participants.isEmpty();
        }

        public Set<String> getParticipants() {
            return Collections.unmodifiableSet(participants);
        }

        public String getRoomId() {
            return roomId;
        }

        public String getCreator() {
            return creator;
        }

        public Date getCreatedAt() {
            return createdAt;
        }
    }
}
